﻿
using System;
using System.IO;

using System.Web.Mvc;
using System.Web.WebPages;
using Microsoft.Practices.Unity;
using Web7.Container.Localization;
using Web7.Container.Services;
using Web7.Core;


namespace Web7.Container.ViewEngines
{
    public abstract class WebViewPage<TModel> : System.Web.Mvc.WebViewPage<TModel>
    {


        private Localizer _localizer;
        //  private IWorkContext _workContext;

        protected ILocalizationService LocalizationService
        {
            get
            {
                return DependencyResolver.Current.GetService<ILocalizationService>();
            }
        }

        public Localizer T
        {
            get
            {

                return _localizer ?? (_localizer = (format, args) =>
                                                       {
                                                           var resFormat = LocalizationService.GetLocalizedString(format);
                                                           if (string.IsNullOrEmpty(resFormat))
                                                           {
                                                               return new LocalizedString(format);
                                                           }
                                                           return
                                                               new LocalizedString((args == null || args.Length == 0)
                                                                                       ? resFormat
                                                                                       : string.Format(resFormat, args));
                                                       });
            }
        }


        public override void InitHelpers()
        {
            base.InitHelpers();
            //cache 
            //if (DataSettingsHelper.DatabaseIsInstalled())
            //{
            //    _localizationService = EngineContext.Current.Resolve<ILocalizationService>();
            //    _workContext = EngineContext.Current.Resolve<IWorkContext>();
            //}

        }

        public HelperResult RenderWrappedSection(string name, object wrapperHtmlAttributes)
        {
            Action<TextWriter> action = delegate(TextWriter tw)
            {
                var htmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(wrapperHtmlAttributes);
                var tagBuilder = new TagBuilder("div");
                tagBuilder.MergeAttributes(htmlAttributes);

                var section = RenderSection(name, false);
                if (section != null)
                {
                    tw.Write(tagBuilder.ToString(TagRenderMode.StartTag));
                    section.WriteTo(tw);
                    tw.Write(tagBuilder.ToString(TagRenderMode.EndTag));
                }
            };
            return new HelperResult(action);
        }

        public HelperResult RenderSection(string sectionName, Func<object, HelperResult> defaultContent)
        {
            return IsSectionDefined(sectionName) ? RenderSection(sectionName) : defaultContent(new object());
        }

        //public override string Layout
        //{
        //    get
        //    {
        //        var layout = base.Layout;

        //        if (!string.IsNullOrEmpty(layout))
        //        {
        //            //var filename = Path.GetFileNameWithoutExtension(layout);
        //            //ViewEngineResult viewResult =Web7.Mvc.ViewEngines.Engines.FindView(ViewContext.Controller.ControllerContext, filename, "");

        //            //if (viewResult.View != null)
        //            //{
        //            //    var razorView = viewResult.View as RazorView;
        //            //    if (razorView != null) layout = razorView.ViewPath;
        //            //}
        //        }

        //        return layout;
        //    }
        //    set
        //    {
        //        base.Layout = value;
        //    }
        //}

        ///// <summary>
        ///// Return a value indicating whether the working language and theme support RTL (right-to-left)
        ///// </summary>
        ///// <returns></returns>
        //public bool ShouldUseRtlTheme()
        //{
        //    var supportRtl = _workContext.WorkingLanguage.Rtl;
        //    if (supportRtl)
        //    {
        //        //ensure that the active theme also supports it
        //        var themeProvider = EngineContext.Current.Resolve<IThemeProvider>();
        //        var themeContext = EngineContext.Current.Resolve<IThemeContext>();
        //        supportRtl = themeProvider.GetThemeConfiguration(themeContext.WorkingDesktopTheme).SupportRtl;
        //    }
        //    return supportRtl;
        //}
    }

    public abstract class WebViewPage : WebViewPage<dynamic>
    {


    }
}
